package com.lanji.sensitive.config;

import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @author zxx
 * @desc autoconfig swagger
 * @createTime 2019-02-22-下午 12:52
 */
@EnableSwagger2
@EnableConfigurationProperties(value = SwaggerProperties.class)
@Configuration
@Slf4j
@Profile({"test", "dev"})
public class SwaggerAutoConfiguration {
    @Autowired
    SwaggerProperties swaggerProperties;

    @Bean
    public ApiInfo apiInfo() {
        log.warn("----->>配置swagger");
        return new ApiInfoBuilder().title(swaggerProperties.getAppName() + ":服务接口文档")
                .description(swaggerProperties.getDesc())
                .termsOfServiceUrl("http://" + swaggerProperties.getIpAddress() + ":" + swaggerProperties.getAppPort() + "/")
                .contact(new Contact("zxx", "www.baidu.com", "zxxcof@qq.com"))
                .version(swaggerProperties.getVersion())
                .build();
    }

    @Bean
    public Docket createRestApi(ApiInfo apiInfo) {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo)
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .paths(PathSelectors.any())
                .build();
    }
}
